<template>
  <a-radio-group v-model:value="radioValue">
    <a-radio :style="radioStyle" :value="1">分钟，允许的通配符[, - * /]</a-radio>
    <a-radio :style="radioStyle" :value="2">
      周期从
      <a-input-number v-model:value="cycle01" :min="0" :max="58" style="width: 130px; display: block-inline" />
      -
      <a-input-number
        v-model:value="cycle02"
        :min="cycle01 + 1"
        :max="59"
        style="width: 130px; display: block-inline"
      />
      分钟
    </a-radio>
    <a-radio :style="radioStyle" :value="3">
      从
      <a-input-number v-model:value="average01" :min="0" :max="58" style="width: 130px; display: block-inline" />
      分钟开始，每
      <a-input-number
        v-model:value="average02"
        :min="1"
        :max="59 - average01"
        style="width: 130px; display: block-inline"
      />
      分钟执行一次
    </a-radio>
    <a-radio id="area" :style="radioStyle" :value="4">
      指定
      <span
        @click="
          (e) => {
            e.preventDefault();
            e.stopPropagation();
          }
        "
      >
        <a-select
          v-model:value="checkboxList"
          mode="multiple"
          :allow-clear="true"
          style="width: 270px; display: block-inline"
          placeholder="可多选"
        >
          <a-select-option v-for="item in 60" :key="item" :label="item - 1" :value="item - 1" />
        </a-select>
      </span>
    </a-radio>
  </a-radio-group>
</template>
<script setup>
const emit = defineEmits(['update']);
const props = defineProps({
  /** 传入的cron表达式对象 */
  cron: {
    type: Object,
    default() {
      return {
        second: '*',
        min: '*',
        hour: '*',
        day: '*',
        month: '*',
        week: '?',
        year: ''
      };
    }
  },
  /** 校验数字格式 */
  check: {
    type: Function,
    default() {
      return;
    }
  }
});
const radioStyle = reactive({
  display: 'flex',
  height: '50px',
  lineHeight: '50px'
});
const radioValue = ref(1);
const cycle01 = ref(0);
const cycle02 = ref(1);
const average01 = ref(0);
const average02 = ref(1);
const checkboxList = ref([]);
const checkCopy = ref([0]);
const cycleTotal = computed(() => {
  cycle01.value = props.check(cycle01.value, 0, 58);
  cycle02.value = props.check(cycle02.value, cycle01.value + 1, 59);
  return `${cycle01.value}-${cycle02.value}`;
});
const averageTotal = computed(() => {
  average01.value = props.check(average01.value, 0, 58);
  average02.value = props.check(average02.value, 1, 59 - average01.value);
  return `${average01.value}/${average02.value}`;
});
const checkboxString = computed(() => checkboxList.value.join(','));
watch(
  () => props.cron.min,
  (value) => changeRadioValue(value)
);
watch([radioValue, cycleTotal, averageTotal, checkboxString], () => onRadioChange());
/**
 * 根据传入的值修改单选按钮
 * @param {String} value 传入的值
 */
function changeRadioValue(value) {
  if (value === '*') {
    radioValue.value = 1;
  } else if (value.indexOf('-') > -1) {
    const indexArr = value.split('-');
    cycle01.value = Number(indexArr[0]);
    cycle02.value = Number(indexArr[1]);
    radioValue.value = 2;
  } else if (value.indexOf('/') > -1) {
    const indexArr = value.split('/');
    average01.value = Number(indexArr[0]);
    average02.value = Number(indexArr[1]);
    radioValue.value = 3;
  } else {
    checkboxList.value = [...new Set(value.split(',').map((item) => Number(item)))];
    radioValue.value = 4;
  }
}
/** 单选按钮值变化时 */
function onRadioChange() {
  switch (radioValue.value) {
    case 1:
      emit('update', 'min', '*', 'min');
      break;
    case 2:
      emit('update', 'min', cycleTotal.value, 'min');
      break;
    case 3:
      emit('update', 'min', averageTotal.value, 'min');
      break;
    case 4:
      if (checkboxList.value.length === 0) {
        checkboxList.value.push(checkCopy.value[0]);
      } else {
        checkCopy.value = checkboxList.value;
      }
      emit('update', 'min', checkboxString.value, 'min');
      break;
  }
}
</script>

<style lang="less" scoped>
.a-input-number--small,
.a-select,
.a-select--small {
  margin: 0 0.2rem;
}
.a-select,
.a-select--small {
  width: 19.8rem;
}
</style>
